package com.myacg.editor.common.utils;

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.text.StyledEditorKit.FontFamilyAction;

public class FontUtils {
	public static String  fontFamily = "方正兰亭超细黑简体";
	
	 
	
	
	/**
	 * 根据字体尺寸居中展示
	 * @param content
	 * @param fontSize
	 * @param comp
	 * @param g
	 */
	public static void drawCenterString(String content ,int fontSize ,JComponent comp,Graphics g){
		int width = comp.getWidth();
		int height = comp.getHeight();
		//字体所占空间
		int fontWidth=fontSize/3*2*content.length();
		int y=(height/2)+fontSize/3*1;
		int x=(width-fontWidth)/2;
		//System.out.println("fontSize:"+fontSize+" x:"+x+" y:"+y);
		g.drawString(content, x, y);
	}
	
	/**
	 * 根据与屏幕占比绘制居中的字符
	 * @param content
	 * @param percentage
	 * @param comp
	 * @param g
	 */
	public static void drawCenterStringByPercentage(String content ,double percentage ,JComponent comp,Graphics g){
		//以宽为基准计算字符占屏幕百分比
		int fontSize = (int)(comp.getWidth()*percentage/content.length());
		 g.setFont(new Font(fontFamily, g.getFont().getStyle(), fontSize/3*2));
		//画出居中字符串
		FontUtils.drawCenterString(content, fontSize, comp, g);
	}
}
